package com.vq3.digitalizationsoftwareintegrates.Service.User.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_SysAuthAuditMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.MongoDB_S_C_WebSocketMessageEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.User_RoleEntity;
import com.vq3.digitalizationsoftwareintegrates.Mapper.Spot_Check.Group_TBMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.User.UserMapper;
import com.vq3.digitalizationsoftwareintegrates.Mapper.User.User_RoleMapper;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_Point_ContentDao;
import com.vq3.digitalizationsoftwareintegrates.Service.MessageCenter.MessageCenterService;
import com.vq3.digitalizationsoftwareintegrates.Service.User.UserLoginService;
import org.springframework.dao.DataAccessException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserLoginServiceImpl implements UserLoginService {
    @Resource
    UserMapper userMapper;
    @Resource
    User_RoleMapper user_roleMapper;
    @Resource
    MongoDB_S_C_Point_ContentDao mongoDB_s_c_point_contentDao;
    @Resource
    Group_TBMapper group_tbMapper;
    @Resource
    MessageCenterService messageCenterService;
    /**加密用的
     * */
    @Resource
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Override
    public UserEntity findUser(String username) {
        UserEntity userEntity = new UserEntity();
        QueryWrapper<UserEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uname",username);

        try {
        userEntity = userMapper.selectOne(userQueryWrapper);
        }catch (DataAccessException e) {
            return userEntity;
        }
        return userEntity;
    }
    public List<User_RoleEntity> findUser_Role(int U_RId) {
        QueryWrapper<User_RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("urole_id",U_RId);
        return user_roleMapper.selectList(queryWrapper);
    }
    public List<UserEntity> uridFindUserName(int uid) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        return userMapper.selectList(queryWrapper);
    }
    public List<User_RoleEntity> uidFindUser_Role(int uid){
        QueryWrapper<User_RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",uid);
        return user_roleMapper.selectList(queryWrapper);
    }
    public List<MongoDB_S_C_WebSocketMessageEntity> findMongoDB_S_C_WebSocketMessage(Permission_TBEntity permission_tbEntity) {
        return mongoDB_s_c_point_contentDao.findMongoDB_S_C_WebSocketMessage(permission_tbEntity);

    }

    public boolean checkUserId(String userId) {
        QueryWrapper<UserEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uname",userId);
        List<UserEntity> userEntities = userMapper.selectList(userQueryWrapper);
        System.out.println(userId);
        return userEntities.isEmpty();
    }
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUser(String userId,String password) {
        UserEntity userEntity = new UserEntity();
        User_RoleEntity user_roleEntity = new User_RoleEntity();
        String encode = bCryptPasswordEncoder.encode(password);
        QueryWrapper<UserEntity> userQueryWrapper = new QueryWrapper<>();
        userEntity.setUname(userId);
        userEntity.setUpassword(encode);
        userEntity.setSysAuthentication("NO");
        userEntity.setDeletestatus(1);
        userEntity.setGroupId(0);
        int insert = userMapper.insert(userEntity);
        userQueryWrapper.eq("uname",userId);
        UserEntity userEntity1 = userMapper.selectOne(userQueryWrapper);
        user_roleEntity.setUserId(userEntity1.getUid());
        user_roleEntity.setUroleId(6);
        int insert1 = user_roleMapper.insert(user_roleEntity);


        return insert > 0 || insert1 > 0;
    }

    public boolean SysAuthentication(MongoDB_S_C_SysAuthAuditMessageEntity messageEntity) {
        return mongoDB_s_c_point_contentDao.SysAuthentication(messageEntity);
    }

    @Override
    public UserEntity uidFindUser(Integer userId) {

        QueryWrapper<UserEntity> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("uid",userId);
        return userMapper.selectOne(userQueryWrapper);
    }


}
